<template>
	<view class="animated fadeIn faster">
		<view class="bg-fff" style="height: 100vh;">
			<view class="d-flex d-flex-center" style="padding-top: 200rpx;">
				<image src="https://www.aqniukt.com/h5/static/images/noLoginEmpty.png" style="width: 320rpx;height:410rpx"></image>
			</view>
			<view class="d-flex d-flex-center mb-3">
				<text class="colorgrayy">登录后查看更多信息</text>
			</view>
			<view class="d-flex d-flex-center">
				<button class="baseBtn" style="width: 440rpx;" @click="getUserInfo">立即登录</button>
			</view>
			<view class="mt-2 d-flex d-flex-middle d-flex-center line-height0">
				<checkbox :checked="isChecked" color="#c90101" style="transform:scale(0.6)" @click="isChecked = !isChecked"></checkbox>
				<text class="font22 color999" @click="isChecked = !isChecked">已阅读并同意</text>
				<text class="font22 main-color" @click="privacy">《用户协议》</text>
				<text class="font22 color999">和</text>
				<text class="font22 main-color" @click="privacy">《隐私条款》</text>
			</view>
		</view>
		<uni-popup ref="wxloginpopup" type="bottom" :safe-area='false'>
			<view class="container">
				<view style="margin-left: 30rpx;width: calc(100% - 60rpx);">
					<view class="title">
						<text>获取昵称头像</text>
						<text class="thod">未选择头像则为默认头像</text>
					</view>
					<view class="choose-avatar-row">
						<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
							<image class="avatar rounded" :src="form.avatar"></image>
							<text class="ml-2 color999">点击选择头像</text>
						</button>
					</view>
					<form name="nickForm" @submit="submit">
						<view class="choose-nickname-row">
							<text class="mr-2">昵称</text>
							<input id="nickName" name="nickname" type="nickname" placeholder="请输入昵称" />
						</view>
						<view class="login-row">
							<button class="btton colorccc" @click="closepopup">关闭</button>
							<button class="btton main-color" form-type="submit">完成</button>
						</view>
					</form>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { fileApi } from '@/http/fileApi';
	import { userApi } from '@/http/userApi';
	import store from '@/store/index.js';
	import { manner } from "@/util/manner.js";
	import { USER_TOKEN } from '@/constants/local-storage-key-const';
	export default {
		data() {
			return {
				isChecked:false,
				form:{
					avatar: 'https://168809.oss-cn-hangzhou.aliyuncs.com/community/avatar.png',
					name: '',
					openid: ''
				}
			}
		},
		methods: {
			getUserInfos(){
				this.$refs.wxloginpopup.open();
			},
			getUserInfo(){
				let that = this
				if(!that.isChecked){
					uni.showToast({
						title:'请先同意用户隐私协议',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:'授权登录中...'
				})
				uni.getUserProfile({
					desc: '登录后可同步数据',
					success: async (obj) => {
						uni.login({
							provider: 'weixin',
							success: async (res) => {
								try{
									const data = await userApi.loginByMini({ code: res.code })
									uni.hideLoading()
									uni.setStorageSync(USER_TOKEN, data.token)
									if(data.is_login){
										uni.setStorageSync('myId', data.user_id)
										store.commit('setLoginInfo',{
											'name':data.user_name,
											'openId':data.openid,
											'avatar':data.user_avatar,
											'verify':data.verify
										})
										manner.goBack()
									}else{
										that.form.openid = data.openid
										that.$refs.wxloginpopup.open()
									}
								}catch(e){
									uni.hideLoading()
								}
							},
							fail:()=>{
								uni.hideLoading()
							}
						})
					},
					fail: () => {
						uni.showToast({
							title: '授权已取消',
							icon: 'error',
							mask: true
						})
					},
				})
			},
			async onChooseAvatar(e) {
				let avatarUrl=e.detail.avatarUrl
				this.uploadImage(avatarUrl)
			},
			async uploadImage(filePath) {
				let that = this
				try{
					uni.showLoading({
						title:'头像上传中...',
					})
					const resImg = await fileApi.upload(filePath,{})
					that.form.avatar = resImg.data
				}catch(e){
					
				}finally{
					uni.hideLoading()
				}
			},
			closepopup(){
				this.$refs.wxloginpopup.close()
			},
			async submit(e){
				let that = this
				that.form.name = e.detail.value.nickname?e.detail.value.nickname:'微信用户'
				try{
					uni.showLoading({
						title:'保持用户信息'
					})
					let userInfo = await userApi.loginSave(that.form)
					uni.setStorageSync('myId', userInfo.user_id)
					store.commit('setLoginInfo',{
						'name':that.form.name,
						'openId':that.form.openid,
						'avatar':that.form.avatar,
						'verify':userInfo.verify
					})
					manner.goBack()
				}catch(e){
					//TODO handle the exception
				}finally{
					uni.hideLoading()
				}
			},
			privacy(){
				uni.navigateTo({
					url:'/pages/privacy/privacy'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.container {
	background-color: #FFFFFF;
	border-radius:25rpx 25rpx 0 0;
	min-height: 500rpx;
	width: 100%;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
	background-color: #fff;
	border-top-left-radius: 20rpx;
	border-top-right-radius: 20rpx;
	.title {
		width: 100%;
		height: 30%;
		font-size: 40rpx;
		font-weight: bold;
		padding-top: 40rpx;
		padding-bottom: 20rpx;
		.thod {
			display: block;
			font-size: 28rpx;
			font-weight: normal;
			margin-top: 10rpx;
		}
	}
	.choose-avatar-row{
		width: 100%;
		height: 20%;
		padding: 20rpx 5rpx;
		font-size: 28rpx;
		border-top: 2rpx solid #ccc;
		border-bottom: 2rpx solid #ccc;
		.avatar-wrapper {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.avatar {
				width: 70rpx;
				height: 70rpx;
					
			}
		}
	}
	.choose-nickname-row {
		width: 100%;
		height: 20%;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx 5rpx;
		font-size: 28rpx;
		border-top: 2rpx solid #ddd;
		border-bottom: 2rpx solid #ddd;
	}
	.choose-nickname-row {
		border-top: none;
	}
	.login-row {
		width: calc(100% - 120rpx);
		margin-left: 60rpx;
		height: 30%;
		padding-top: 40rpx;
		padding-bottom: 100rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		.btton {
			font-size: 28rpx;
			width: 40%;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.inactive {
			color: #ccc;
		}
	}
}
</style>
